<?php

class Signup_form_mvc extends CI_Controller {

	public function __construct() {
		parent::__construct(); 
                // the tutorial shows this as 
                // parent::Controller();
                
                $this->load->helper(array('form','url'));
                $this->load->library('form_validation');
	}
        
        public function index() {
		$this->load->view('testing/signup_form_mvc_view');
        }

        
        
        public function submit() {
            
            if ($this->_submit_validate() === FALSE) {
                $this->index();
                return;    
            }
            
            $this->load->model('testing/signup_form_mvc_model');
            $this->signup_form_mvc_model->commit_new_user();
	    
            /*
            $user = new Entities\User;
            $user->setFirstName($this->input->post('firstname'));
            $user->setLastName($this->input->post('lastname'));
            $user->setPassword(md5($this->input->post('password')));
            $user->setEmail($this->input->post('email'));
            $user->setWebsite($this->input->post('website'));
            $user->setCreated(new DateTime());
            
            // standard way in CodeIgniter to access a library in a controller: $this->library_name->member->memberFunction()
            // save the object to database, so that it can obtain its ID
            $this->doctrine->em->persist($user);
            
            $this->doctrine->em->flush();
            
             * 
             */
            $this->load->view('testing/signup_form_success_view');
        }
        
        private function _submit_validate() {
            
            $this->form_validation->set_rules('firstname', 'Firstname',
                    'required|min_length[2]|trim|max_length[15]|alpha|xss_clean');
            
            $this->form_validation->set_rules('lastname', 'Lastname',
                    'required|min_length[2]|trim|max_length[15]|alpha|xss_clean');

             $this->form_validation->set_rules('password', 'Password',
                     'required|min_length[6]|max_length[24]');

             $this->form_validation->set_rules('passconf', 'Confirm Password',
                     'required|matches[password]');

             //$this->form_validation->set_rules('email', 'E-mail',
               //   'required|valid_email|is_unique[users.email]');
             
             $this->form_validation->set_rules('email', 'E-mail', 
                     'required|valid_email|min_length[8]|trim|callback_email_check');
             
             $this->form_validation->set_rules('website', 'Website',
                    'min_length[6]|trim|max_length[20]|xss_clean');

             return $this->form_validation->run();

        }
        
        public function email_check($str)
	{
		$user = $this->doctrine->em->getRepository('ORM\Testing\User')->findOneBy(array('email' => $str));
                if ($str == 'test')
		{
			$this->form_validation->set_message('email_check', 'The %s field can not be the word "test"');
			return FALSE;
		}
		else if (isset($user)) {
                        $this->form_validation->set_message('email_check', 'This email address is already in use');
                        return FALSE;
                } else
		{
			return TRUE;
		}
	}

        
        
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
